package com.jar.exercise.多线程.死锁代码;

import com.jar.exercise.多线程.TheadTools;

public class App {

    static final Object key0 = new Object();
    static final Object key1 = new Object();


    public static void main(String[] args) {


        TheadTools.getThread(() -> {
            try {
                new A().p0();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "tn0").start();


        TheadTools.getThread(() -> {
            try {
                new B().p0();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "tn1").start();

    }
}


class A {
    public void p0() throws InterruptedException {
        synchronized (App.key0){
            Thread.sleep(10000);
            synchronized (App.key1){
                System.out.println(1);
            }
        }
    }
}

class B {
    public void p0() throws InterruptedException {

        synchronized (App.key1){
            Thread.sleep(10000);
            synchronized (App.key0){
                System.out.println(1);
            }
        }

    }
}
